<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  User: 黄星
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/common.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/question-add.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/page/admin/static/h-ui.admin/skin/default/skin.css" id="skin" />

    <script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/jquery/1.9.1/jquery.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/jquery.sPage.js"></script>
    <script type="text/javascript"  src="${pageContext.request.contextPath}/page/admin/static/h-ui/js/H-ui.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/static/h-ui.admin/js/H-ui.admin.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/layer/2.4/layer.js"></script>
    <script type="text/javascript">
        /*初始化*/
        $(function () {
            majorSelect();
            classesSelect();
            subjectSelect();
            typeSelect();
        });

        /*专业下拉框变化*/
        function majorSelect() {
            $("#majorSelect").change(function () {
                /*将题目类型的空给隐藏*/
                $("#Choice").css("display", "none");
                $("#Blank").css("display", "none");
                $("#Judge").css("display", "none");
                /*获取专业ID*/
                var majorId = $("#majorSelect option:selected").val();
                if (majorId == 0) {
                    /*为0 就重置所有的下拉框*/
                    $("#classesSelect").empty();
                    $("#classesSelect").append(
                        '<option value="0">请选择年级</option>');
                    $("#classesSelect").val(0);
                    $("#subjectSelect").empty();
                    $("#subjectSelect").append(
                        '<option value=0>请选择科目</option>'
                    );
                    $("#subjectSelect").val(0);
                    $("#typeSelect").empty();
                    $("#typeSelect").append(
                        '<option value=0>请选择题型</option>'
                    );
                    $("#typeSelect").val(0);
                } else {
                    /*否则  重置科目题型下拉框   初始化年级下拉框*/
                    $("#classesSelect").empty();
                    $("#classesSelect").append(
                        '<option value=0>请选择年级</option>'
                    );
                    $("#classesSelect").append(
                        '<option value=1>大一</option>'
                    );
                    $("#classesSelect").append(
                        '<option value=2>大二</option>'
                    );
                    $("#classesSelect").append(
                        '<option value=3>大三</option>'
                    );
                    $("#classesSelect").append(
                        '<option value=4>大四</option>'
                    );
                    $("#classesSelect").val(0);
                    $("#subjectSelect").empty();
                    $("#subjectSelect").append(
                        '<option value=0>请选择科目</option>'
                    );
                    $("#subjectSelect").val(0);
                    $("#typeSelect").empty();
                    $("#typeSelect").append(
                        '<option value=0>请选择题型</option>'
                    );
                    $("#typeSelect").val(0);

                }
            })
        }

        /*班级下拉框变化*/
        function classesSelect() {
            $("#classesSelect").change(function () {
                /*将题目类型的空给隐藏*/
                $("#Choice").css("display", "none");
                $("#Blank").css("display", "none");
                $("#Judge").css("display", "none");
                /*获取专业，班级ID  查询科目*/
                var majorId = $("#majorSelect option:selected").val();
                var gradeId = $("#classesSelect option:selected").val();
                $.ajax({
                    url: "${pageContext.request.contextPath}/QuestionManage/getSubjectByMajorAndClasses",
                    type: "get",
                    dataType: "json",
                    data: {"majorId": majorId, "gradeId": gradeId},
                    async: false,
                    success: function (paging) {
                        $("#subjectSelect").empty();
                        $("#subjectSelect").append(
                            '<option value=0>请选择科目</option>'
                        );
                        $.each(paging.list, function (index, subjectList) {
                            $("#subjectSelect").append(
                                '<option value=' + subjectList.id + '> ' + subjectList.subjectName + '</option>'
                            );
                        });
                        $("#subjectSelect").val(0);
                        $("#typeSelect").empty();
                        $("#typeSelect").append('<option value="0">请选择题型</option>');
                        $("#typeSelect").val(0);
                    },
                    error: function () {
                        alert('error');
                    }
                });

            })
        }

        /*科目下拉框变化*/
        function subjectSelect() {
            $("#subjectSelect").change(function () {
                /*将题目类型的空给隐藏*/
                $("#Choice").css("display", "none");
                $("#Blank").css("display", "none");
                $("#Judge").css("display", "none");
                var subjectId = $("#subjectSelect").val();
                if (subjectId == 0) {
                    /*为空就重置题型下拉框*/
                    $("#typeSelect").empty();
                    $("#typeSelect").append('<option value="0">请选择题型</option>')
                } else {
                    $("#typeSelect").append('<option value="1">选择题</option>');
                    $("#typeSelect").append('<option value="2">填空题</option>');
                    $("#typeSelect").append('<option value="3">判断题</option>')
                }
            })
        }

        /*题型下拉框变化*/
        function typeSelect() {
            //用来选择显示哪个题目添加框
            $("#typeSelect").change(function () {
                var typeId = $("#typeSelect").val();
                if (typeId == 0) {
                    $("#Choice").css("display", "none");
                    $("#Blank").css("display", "none");
                    $("#Judge").css("display", "none");
                } else {
                    if (typeId == 1) {
                        $("#Choice").css("display", "block");
                        $("#Blank").css("display", "none");
                        $("#Judge").css("display", "none");
                    } else if (typeId == 2) {
                        $("#Choice").css("display", "none");
                        $("#Blank").css("display", "block");
                        $("#Judge").css("display", "none");
                    } else {
                        $("#Choice").css("display", "none");
                        $("#Blank").css("display", "none");
                        $("#Judge").css("display", "block");
                    }
                }
            })
        }

        /*添加题目页面点击确定后*/
        function addClick() {
            var submit = false;
            var typeId = $("#typeSelect").val();
            var data = "typeId=" + typeId + "&subjectId=" + $("#subjectSelect").val();
            if (typeId == 0) {
                layer.msg("请先选择要添加题目的信息",{icon:2,time:2000});

            } else if (typeId == 1) {
                /*选择的为选择题   获取各个的值，进行请求参数的拼接*/
                var choiceTopic = $("#choiceTopic").val();
                var choiceAnswer = $("#choiceAnswer").val();
                var choiceErrAnswer1 = $("#choiceErrAnswer1").val();
                var choiceErrAnswer2 = $("#choiceErrAnswer2").val();
                var choiceErrAnswer3 = $("#choiceErrAnswer3").val();
                if (!isEmptyString(choiceTopic) && !isEmptyString(choiceAnswer) && !isEmptyString(choiceErrAnswer1) && !isEmptyString(choiceErrAnswer2) && !isEmptyString(choiceErrAnswer3)) {
                    if(!errAnswerIsSame(choiceErrAnswer1,choiceErrAnswer2,choiceErrAnswer3)){
                        submit = true;
                        data += "&choiceTopic=" + choiceTopic;
                        data += "&choiceAnswer=" + choiceAnswer;
                        data += "&choiceErrAnswer1=" + choiceErrAnswer1;
                        data += "&choiceErrAnswer2=" + choiceErrAnswer2;
                        data += "&choiceErrAnswer3=" + choiceErrAnswer3;
                    }else{
                        layer.msg("三个错误答案必须不同",{icon:2,time:2000});
                    }
                }else {
                    layer.msg("还有未填写的内容，请填写完整",{icon:2,time:2000});
                }
            } else if (typeId == 2) {
                /*选择的为填空题   获取各个的值，进行请求参数的拼接*/
                var blankBeforeTopic = $("#blankBeforeTopic").val();
                var blankAfterTopic = $("#blankAfterTopic").val();
                var blankAnswer = $("#blankAnswer").val();
                //如果空前为空
                if(isEmptyString(blankBeforeTopic)){
                    if(isEmptyString(blankAfterTopic)){
                        //如果空后为空   就显示提示信息
                        layer.msg("空前和空后题目必填其一",{icon:2,time:2000});
                    }else{
                        //判断答案
                        if(!isEmptyString(blankAnswer)){
                            //不为空就添加
                            submit = true;
                            data += "&blankBeforeTopic=" + blankBeforeTopic;
                            data += "&blankAfterTopic=" + blankAfterTopic;
                            data += "&blankAnswer=" + blankAnswer;
                        }
                    }
                }else{
                    //空前不为空  就判断答案
                    if(!isEmptyString(blankAnswer)){
                        //不为空就添加
                        submit = true;
                        data += "&blankBeforeTopic=" + blankBeforeTopic;
                        data += "&blankAfterTopic=" + blankAfterTopic;
                        data += "&blankAnswer=" + blankAnswer;
                    }
                }
            } else {
                /*选择的为判断题   获取各个的值，进行请求参数的拼接*/
                var judgeTopic = $("#judgeTopic").val();
                var judgeErrAnswer = $("#judgeErrAnswer").val();
                if(!isEmptyString(judgeTopic) && !isEmptyString(judgeErrAnswer)){
                    submit = true;
                    data += "&judgeTopic=" + judgeTopic;
                    data += "&judgeErrAnswer=" + judgeErrAnswer;
                }
            }
            if (submit) {
                $.ajax({
                    url: "${pageContext.request.contextPath}/QuestionManage/addQuestion",
                    type: "get",
                    dataType: "text",
                    data: data,
                    async: false,
                    success: function (message) {
                        parent.layer.msg(message,{icon:1,time:2000});
                        parent.document.getElementById("updFlash").click();
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    },
                    error: function (message) {
                        parent.layer.msg(message,{icon:2,time:2000});
                    }
                });
            } else {
                if(typeId == 3){
                    layer.msg('还有未填写的内容，请填写完整',{icon:2,time:2000});
                }
            }
        }
        // 判断是否是空串
        function isEmptyString(str) {
            if (str == "") {
                return true;
            } else {
                return false;
            }
        }
        /*判断选择题三个错误答案是否一样*/
        function errAnswerIsSame(err1,err2,err3) {
            if(err1.trim() == err2.trim() || err1.trim() == err3.trim() || err2.trim() == err3.trim()){
                return true;
            }
            else {
                return false;
            }
        }
    </script>
</head>
<body>
<form>

    <div class="left question-add-main">
        <%--显示专业--%>
        <div class="left question-add-select">
            专业：
            <select id="majorSelect">
                <option value="0">请选择专业</option>
                <c:forEach items="${majorList}" var="major">
                    <option value="${major.id}">${major.majorName}</option>
                </c:forEach>
            </select>
        </div>
        <%--显示年级--%>
        <div class="left question-add-select">
            年级：
            <select id="classesSelect">
                <option value="0">请选择年级</option>
            </select>
        </div>
        <%--显示科目--%>
        <div class="left question-add-select">
            科目：
            <select id="subjectSelect">
                <option value="0">请选择科目</option>
            </select>
        </div>
        <%--显示题型--%>
        <div class="left question-add-select">
            题型：
            <select id="typeSelect">
                <option value="0">请选择题型</option>
            </select>
        </div>
        <%--placeholder没输入内容 为空串--%>
            <%--选择题添加框--%>
        <div id="Choice" class="left addChoice">
            <span>*必填</span><input type="text" id="choiceTopic" placeholder="请输入题目"><br/>
            <span>*必填</span><input type="text" id="choiceAnswer" placeholder="请输入正确答案"><br/>
            <span>*必填</span><input type="text" id="choiceErrAnswer1" placeholder="请输入错误答案"><br/>
            <span>*必填</span><input type="text" id="choiceErrAnswer2" placeholder="请输入错误答案"><br/>
            <span>*必填</span><input type="text" id="choiceErrAnswer3" placeholder="请输入错误答案"><br/>
        </div>
            <%--填空题题添加框--%>
        <div id="Blank" class="left addBlank">
            <input type="text" id="blankBeforeTopic" placeholder="请输入在空前的题目，可不输入，但与下框必填一个"><br/>
            <input type="text" id="blankAfterTopic" placeholder="请输入在空后的题目，可不输入，但与上框必填一个"><br/>
            <input type="text" id="blankAnswer" placeholder="请输入答案"><br/>
        </div>
            <%--判断题添加框--%>
        <div id="Judge" class="left addJudge">
            <span>*必填</span><input type="text" id="judgeTopic" placeholder="请输入正确题目"><br/>
            <span>*必填</span><input type="text" id="judgeErrTopic" placeholder="请输入错误题目"><br/>
        </div>
        <div class="left" style="width: 100%;margin-top: 5%;" align="center">
            <input id="sub" type="button" class="question-add-btn" onclick="addClick()" value="确定"/>
            <input type="button" class="question-add-btn" onclick="layer_close()" value="关闭" style="margin-left: 15%;"/>
        </div>
    </div>
</form>
</body>
</html>
